package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.pojo.Activation;
import org.example.service.ActivationService;
import org.example.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @Author: Edward
 * @Date:2022-08-31 14:59
 * @subject:
 * 这个类负责和前端做连接
 * 需要注入service，用他来调用方法
 */
@Controller
@Api(tags = "Activations测试")
@RequestMapping("/activations")
public class ActivationController {
    //controller调用service层
    @Autowired
    private ActivationService activationService;

    @ResponseBody
    @PostMapping("/activationWrite")
    @ApiOperation("编写相关活动")
    public ResponseData write(Activation activation){
        boolean b = activationService.addActivation(activation);
        return b?ResponseData.getSccesss("编写成功",100):ResponseData.error("编写失败", 101);
    }

    @ResponseBody
    @PostMapping("/activationUpdate")
    @ApiOperation("修改相关活动")
    public ResponseData update(Activation activation){
        boolean b = activationService.updateActivation(activation);
        return b?ResponseData.getSccesss("修改成功",100):ResponseData.error("修改失败", 101);
    }

    /**文件上传
     *
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/imgUploadFile")
    @ApiOperation("上传照片")
    @ResponseBody
    public ResponseData imgUploadFile(MultipartFile file, HttpServletRequest request){
        //获取文件上传上传名 加后缀
        String filename = file.getOriginalFilename();

//        获取全路径文件夹
        String upload = request.getServletContext().getRealPath("upload");
        System.out.println(upload);

        File file1=new File(upload);
        //当文件夹不存在的时候创建
        if (!file1.exists()){
            file1.mkdir();
        }
//        使用uuid和时间搓命名
        String imgName= UUID.randomUUID().toString().replaceAll("-","") +System.currentTimeMillis()+filename;

//        文件上传的最终添加的路径
        String endFile=upload+"/"+imgName;
        try {
            file.transferTo(new File(endFile));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return ResponseData.allResponse(imgName,"上传成功",100,0);

    }

    @ResponseBody
    @PostMapping("/activationDelete")
    @ApiOperation("通过id删除相关活动")
    public ResponseData delete(Integer aId){
        boolean b = activationService.deleteActivationById(aId);
        return b?ResponseData.getSccesss("删除成功",100):ResponseData.error("删除失败", 101);
    }

    @ResponseBody
    @GetMapping("/activationAll/{page}/{limit}")
    @ApiOperation("查询所有的相关活动并分页")
    public ResponseData list(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页的数据") @PathVariable int limit){

        PageHelper.startPage(page,limit);
        List<Activation> activations = activationService.showAllActivation();
        PageInfo<Activation> activationPageInfo = new PageInfo<>(activations);
        //以json的格式传递
        ResponseData jsonData = ResponseData.getJsonData(activations, (int) activationPageInfo.getTotal());
        return jsonData;
    }

    @ResponseBody
    @PostMapping("/activationSelect/{page}/{limit}/{a_name}")
    @ApiOperation("根据关键字对相关活动进行模糊查询")
    public ResponseData select(@ApiParam("当前页") @PathVariable int page, @ApiParam("每一页的数据") @PathVariable int limit,String aName){
        PageHelper.startPage(page,limit);
        List<Activation> activations = activationService.selectActivationByName(aName);
        PageInfo<Activation> activationPageInfo = new PageInfo<>(activations);
        //以json格式传递
        ResponseData jsonData = ResponseData.getJsonData(activations, (int) activationPageInfo.getTotal());
        return jsonData;
    }
}
